﻿using System;
using System.Threading;
using ControleRepresentante.Common;
using ControleRepresentante.Migration.Repositories;
using ControleRepresentante.Migration.Steps;

namespace ControleRepresentante.Migration
{
    class Migrator : IDisposable
    {
        private readonly ZTRepository m_ZTRepository;
        private readonly ControleRepresentanteRepository m_ControleRepresentanteRepository;
        internal Thread MigrationThread;
        internal bool IsCancel;

        public MigrationStatus MigrationStatus { get; private set; }
        public event EventHandler End;

        public Migrator()
        {
            MigrationStatus = new MigrationStatus(this)
            {
                CurrentStep = "Not Started",
                CurrentStepProgress = 0
            };

            m_ZTRepository = new ZTRepository();
            m_ControleRepresentanteRepository = new ControleRepresentanteRepository();
        }

        public void Start()
        {
            MigrationThread = new Thread(Migrate);
            MigrationThread.Start();
        }

        public void Cancel()
        {
            IsCancel = true;
        }

        private void Migrate()
        {
            MigrationStatus.CurrentStep = "Started";

            Guid userId;
            Guid representanteId = CreateRepresentante(out userId);

            IMigrationStep[] steps = new IMigrationStep[]
            {
                new FabricaMigrationStep(),
                new ContaMigrationStep(),
                new TransportadoraMigrationStep(),
                new VendedorMigrateStep(),
                new ClienteMigrateStep(),
                new PedidoMigrateStep(),
                new ProdutoMigrateStep(),
                new PedidoCalculationMigrateStep(),
            };

            foreach (IMigrationStep migrationStep in steps)
            {
                migrationStep.Migrate(representanteId, MigrationStatus, m_ControleRepresentanteRepository, m_ZTRepository, userId);
            }

            MigrationStatus.CurrentStep = "Finished";
            MigrationStatus.CurrentStepProgress = 100;
            MigrationStatus.EstimatedTime = TimeSpan.FromMilliseconds(0);

            FireEnd();
        }

        internal void FireEnd()
        {
            if (End != null)
                End(this, EventArgs.Empty);
        }

        private Guid CreateRepresentante(out Guid userId)
        {
            const string Nome = "ZT Representações Comerciais Ltda.";
            const string NomePedido = 
@"ZT Representações 
Comerciais Ltda.";
            const string InfoPedido =
@"Rua República do Líbano 61/1112
Centro - 20061-030 - Rio de Janeiro - RJ
Tel: (21) 2242-7635 / 2252-9529
Fax: 2252-2852 
e-mail: ztjolit@bordallo.com.br";

            Guid representanteId = m_ControleRepresentanteRepository.CreateRepresentante(Nome, true, NomePedido, InfoPedido, true);
            userId = m_ControleRepresentanteRepository.CreateUser(representanteId, "u", "u".Encrypt(), true, false);

            MigrationStatus.CurrentStepProgress = 100;
            return representanteId;
        }

        private Guid GetRepresentante(out Guid userId)
        {
            userId = m_ControleRepresentanteRepository.GetUserId();
            return m_ControleRepresentanteRepository.GetRepresentanteId();
        }

        public void Dispose()
        {
            m_ControleRepresentanteRepository.Dispose();
            m_ZTRepository.Dispose();
        }
    }
}
